
import com.facebook.graph.FacebookMobile;

import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.StageWebView;

import mx.collections.ArrayCollection;
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import mx.events.StateChangeEvent;

private var appID:String = "296274863812790";
public var xmlStorage:XMLStorage;
public var userInfo:UserInfo;
protected function initProcess():void{
	
	
	loginWebView.stage = FlexGlobals.topLevelApplication.stage;
	loginWebView.viewPort = new Rectangle(0, 0, this.width, this.height);
	
	FacebookMobile.init(appID, handleInit);
	
	
	if(getPref()){
		passLogin();
	}else{
		goLogin();
	}
	
}



private function getPref():Boolean{
	
	xmlStorage = new XMLStorage();
	userInfo = xmlStorage.getUserInfo();
	
	if(userInfo != null && userInfo.accessToken != null){
		if(userInfo.accessToken != ''){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
	
	return true;
}

private function handleInit(session:Object, fail:Object):void{
	
}

private function passLogin():void{
	
	//this.currentState = "Account";
	this.currentState = "Timeline";
	
}

private function goLogin():void{
	
	this.currentState = "Login";
	
}

private var loginWebView:StageWebView = new StageWebView();
private function onClickFacebookLogin():void{
	
	
	FacebookMobile.login(handleLogin, stage, ['email','user_birthday','user_about_me','user_status','user_work_history', 'user_activities', 'publish_actions', 'read_friendlists', 'status_update','friends_about_me', 'publish_stream','user_photos','user_videos','offline_access', 'read_stream','friends_activities','user_likes','user_location','friends_location'], loginWebView);
	
}


private function handleLogin(session:Object, fail:Object):void {
	if (session != null) {
		//
		trace("got uid : " + session.uid);
		
		saveFacebookSession(session);
		
	}else if(fail != null) {
		trace("failed : " + fail.toString());
	}
}

private function saveFacebookSession(session:Object):void{
	saveSessionToUserInfo(session);
	saveStorage();
	
	passLogin();
}


private function saveSessionToUserInfo(session:Object):void{
	userInfo = new UserInfo();
	userInfo.setBySession(session);
	
}

private function saveStorage():void{
	xmlStorage.saveByUserInfo(userInfo);
}


protected function onCurrentStateChange(event:StateChangeEvent):void
{
	// TODO Auto-generated method stub
	switch(event.newState){
		case 'Account':
			lab_userName.text = userInfo.username;
			lab_accessToken.text = userInfo.accessToken;
			break;
		case 'Timeline':
			if(list_timeline.dataProvider == null){
				getTimeline();
			}
			break;
		case 'Friends':
			if(list_friendsList.dataProvider == null){
				getFriendsList();
			}
		
	}
}


public function goState(menu:String):void{
	this.currentState = menu;
}

protected function onClickHeader(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	switch(this.currentState){
		case 'Timeline':
			getTimeline();
			break;
		case 'Friends':
			getFriendsList();
			break;
	}
}

protected function getTimeline():void{
	bi_topLoading.visible = true;
	FacebookMobile.fqlQuery("SELECT post_id, created_time, message, likes, comments, attachment, permalink, source_id, actor_id, description FROM stream WHERE filter_key IN (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed' ) limit 30", getTimelineHandler); 
}


protected function getTimelineHandler(result:Object,fail:Object):void{
	if(result != null){
		trace("result : " + result.toString());
		var timeline:ArrayCollection = new ArrayCollection();
		for (var i:int = 0; i <result.length; i++){
			//friendsList.addItem(result[i].first_name + result[i].last_name);
			timeline.addItem(result[i]);
		}
		list_timeline.dataProvider = timeline;
		
	}else{
		trace("failed : " + fail.toString());
	}
	
	bi_topLoading.visible = false;
}

protected function getFriendsList():void{
	bi_topLoading.visible = true;
	FacebookMobile.fqlQuery("SELECT uid, first_name, last_name FROM user WHERE uid in (SELECT uid2 FROM friend where uid1 = me())", getFriendsListHandler); 
	
}

protected function getFriendsListHandler(result:Object, fail:Object):void{
	if(result != null){
		trace("result : " + result.toString());
		var friendsList:ArrayCollection = new ArrayCollection();
		for (var i:int = 0; i <result.length; i++){
			friendsList.addItem(result[i].first_name + result[i].last_name);
			
		}
		list_friendsList.dataProvider = friendsList;
		
	}else{
		trace("failed : " + fail.toString());
	}
	
	bi_topLoading.visible = false;
}